package com.diorsunion.hedge.io;

import java.io.Serializable;
import java.util.Date;

/**
 * 消息封装bean
 * Created by harley-dog on 2016/5/17.
 */
public class Message<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1122341233L;
    public final MessageType type;
    public final String name;
    public final T data;
    public final Date thedate;

    Message(MessageType type, String name,T data) {
        this.type = type;
        this.data = data;
        this.name = name;
        this.thedate = new Date();
    }

    public static final <T extends Serializable> Message<T> genMessage(MessageType type,T obj){
        return new Message<T>(type,type.name(),obj);
    }

    public static final <T extends Serializable> Message<T> genMessage(MessageType type, String name,T obj){
        return new Message<T>(type,name,obj);
    }

    public static final Message<Long> genHeartBeatMessage(){
        return new Message<Long>(MessageType.HeartBeat,MessageType.HeartBeat.name(),System.currentTimeMillis());
    }

    public static final <T extends Serializable> Message<T> genCommandMessage(String name,T obj){
        return new Message<T>(MessageType.Command,name,obj);
    }

    public static final <T extends Serializable> Message<T> genDateMessage(String name,T obj){
        return new Message<T>(MessageType.Data,name,obj);
    }

    public static final <T extends Serializable> Message<T> genResultMessage(String name,T obj){
        return new Message<T>(MessageType.Result,name,obj);
    }


    @Override
    public String toString() {
        return "Message{" +
                "type=" + type +
                ", data=" + data +
                '}';
    }
}
